/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package nl.cloudfarming.client.sensor;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.Date;
import nl.cloudfarming.client.model.SensorData;
import nl.cloudfarming.client.model.SensorDataFile;
import nl.cloudfarming.client.model.SensorMetadata;
import nl.cloudfarming.client.util.MD5Sum;

/**
 *
 * @author Timon Veenstra
 */
public abstract class SensorProcessor<D extends SensorData> {

    /**
     * process a sensor data file
     */
    public abstract void process();

    /*
     * Determine rundate for this sensorfile
     * @param sensorFile File-object representing the sensorfile
     */
    protected abstract Date getDateFromFileName(File sensorFile) throws MalformedFileFormatException, ParseException;


    /*
     * Each sensorProcessor should have its own buildMetadataFromPreferences() method
     */
    protected abstract SensorMetadata buildMetadataFromPreferences();
    
    /**
     * Creates the SensorDataFile object using the file that this fileprocessor should process and using the implemented getRunDate() and buildMetadataFromPreferences() methods
     * 
     * @param sensorFile File-object representing the sensorfile
     * @param metadata The metadata object belonging to this file
     * @return SensorDataFile object
     * @throws MalformedFileFormatException When filename (date-) format does not comply to sensors modules fileformat
     * @throws ParseException When filename (date-) format does not comply to greenseeker fileformat and cannot therefor not be parsed as a valid date
     * @throws NoSuchAlgorithmException When checksum fails
     * @throws IOException When the file this processor needs to process cannot be read.
     */
    protected SensorDataFile createSensorDataFile(File sensorFile) throws MalformedFileFormatException, ParseException, NoSuchAlgorithmException, IOException{
        SensorMetadata metadata = buildMetadataFromPreferences();
        Date runDate = getDateFromFileName(sensorFile);
        String checkSum = MD5Sum.getHex(MD5Sum.createChecksum(new FileInputStream(sensorFile)));
        String fileName = sensorFile.getName();
        return new SensorDataFile(fileName, checkSum, runDate, new Date(), metadata);
    }
    
    /**
     * Concrete subclasses must override this method to return the displayname of that sensorprocessor to be displayed in the log.
     * For example: "ASC-340 Cropcircle"
     * 
     * Primary use is for logging, for example: [Info] Start processing of ASC-340 Cropcircle sensor file 010710AD.CSV
     * 
     * @return 
     */
    protected abstract String getLogDisplayName();    
}
